package com.cz.video.website.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.video.common.constant.UserConstants;
import com.cz.video.common.core.domain.PageQuery;
import com.cz.video.common.core.page.TableDataInfo;
import com.cz.video.common.helper.LoginHelper;
import com.cz.video.common.utils.StringUtils;
import com.cz.video.website.domain.WebsiteCollectFolder;
import com.cz.video.website.domain.bo.WebsiteCollectFolderBo;
import com.cz.video.website.domain.vo.WebsiteCollectFolderVo;
import com.cz.video.website.mapper.WebsiteCollectAndFolderMapper;
import com.cz.video.website.mapper.WebsiteCollectFolderMapper;
import com.cz.video.website.service.IWebsiteCollectFolderService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 收藏夹表 服务实现类
 * </p>
 *
 * @author yr
 * @since 2023-10-16
 */
@Service
@RequiredArgsConstructor
public class WebsiteCollectFolderServiceImpl extends ServiceImpl<WebsiteCollectFolderMapper, WebsiteCollectFolder> implements IWebsiteCollectFolderService {
    private final WebsiteCollectFolderMapper websiteCollectFolderMapper;
    private final WebsiteCollectAndFolderMapper websiteCollectAndFolderMapper;
    /**
     *新增
     */
    @Override
    public int insertCollectFolder(WebsiteCollectFolder websiteCollectFolder) {
        int rows = websiteCollectFolderMapper.insert(websiteCollectFolder);
        return rows;
    }
    /**
     * 校验收藏夹名称是否唯一
     *
     * @param websiteCollectFolder 用户信息
     * @return 结果
     */
    @Override
    public boolean checkUserNameUnique(WebsiteCollectFolder websiteCollectFolder) {
        boolean exist = websiteCollectFolderMapper.exists(new LambdaQueryWrapper<WebsiteCollectFolder>()
//                .eq(WebsiteCollectFolder::getDelFlag, CommonConstants.NOT_DELETED)
            .eq(WebsiteCollectFolder::getName, websiteCollectFolder.getName())
            .eq(WebsiteCollectFolder::getUserId, websiteCollectFolder.getUserId())
            .ne(websiteCollectFolder.getCollectFolderId()!=null,WebsiteCollectFolder::getCollectFolderId,websiteCollectFolder.getCollectFolderId()));
        return !exist;
    }

    /**
     * 删除收藏夹
     * @param id
     * @return
     */
    @Override
    public int deleteById(Long[] id) {
        return websiteCollectFolderMapper.delete(Wrappers.<WebsiteCollectFolder> lambdaQuery()
            .eq(WebsiteCollectFolder::getUserId, LoginHelper.getUserId())
            .in(WebsiteCollectFolder::getCollectFolderId, (Object[]) id));
    }

    /**
     * 修改收藏夹名称
     * @param websiteCollectFolder
     * @return
     */
    @Override
    public int updateCollectFolder(WebsiteCollectFolder websiteCollectFolder) {
        return websiteCollectFolderMapper.updateById(websiteCollectFolder);
    }

    @Override
    public TableDataInfo<WebsiteCollectFolderVo> selectPageCollectFolderList(WebsiteCollectFolderBo websiteCollectFolder, PageQuery pageQuery) {
        Page<WebsiteCollectFolderVo> page = websiteCollectFolderMapper.selectPageCollectFolderList(pageQuery.build(),
            this.buildQueryWrapper(websiteCollectFolder),websiteCollectFolder,LoginHelper.getUserId());
        return TableDataInfo.build(page);
    }

    private Wrapper<WebsiteCollectFolder> buildQueryWrapper(WebsiteCollectFolder websiteCollectFolder) {
        Map<String, Object> params = websiteCollectFolder.getParams();
        QueryWrapper<WebsiteCollectFolder> wrapper = Wrappers.query();

        wrapper.eq("cf.del_flag", UserConstants.USER_NORMAL)
            .eq(ObjectUtil.isNotNull(websiteCollectFolder.getCollectFolderId()), "cf.collect_folder_id", websiteCollectFolder.getCollectFolderId())
            .like(StringUtils.isNotBlank(websiteCollectFolder.getName()), "cf.name", websiteCollectFolder.getName())
            .eq(ObjectUtil.isNotNull(websiteCollectFolder.getUserId()), "cf.user_id", websiteCollectFolder.getUserId())
            .between(params.get("beginTime") != null && params.get("endTime") != null,
                "cf.create_time", params.get("beginTime"), params.get("endTime"));
        return wrapper;
    }
}
